(function(){
	
	var pubSubListeners = {};

	var listenPubSub = function(name, callback)
	{
		var l = pubSubListeners[name];
		if(!l)
			l = pubSubListeners[name] = [];
		
		l.push(callback);
	};

	var listenDOM = function(target, name, callback)
	{
		var evtWrapper =
		{
			nativeEvt: null,
			
			stop: function()
			{
				if(this.nativeEvt.preventDefault)
				{
					this.nativeEvt.preventDefault();
					this.nativeEvt.stopPropagation();
				}
				else
				{
					this.nativeEvt.returnValue = false;
					this.nativeEvt.cancelBubble = true;
				}
			},
			
			getTarget: function()
			{
				return this.nativeEvt.target || this.nativeEvt.srcElement;
			}
		};
		
		var callbackWrapper = function(nativeEvt)
		{
			evtWrapper.nativeEvt = nativeEvt;
			return callback(evtWrapper);
		};
		
		if(target.addEventListener)
			target.addEventListener(name, callbackWrapper, false, true);
		else if(target.attachEvent)
			target.attachEvent("on"+name, callbackWrapper);
	};
	
	phnq.event =
	{
		listen: function()
		{
			if(arguments.length < 2)
				throw "phnq.event.listen() takes at least 2 args.";
			
			if(typeof(arguments[0]) == "string")
			{
				listenPubSub.apply(null, arguments);
			}
			else if(arguments.length > 2)
			{
				listenDOM.apply(null, arguments);
			}
		},
		
		post: function(name, userData)
		{
			var responses = [];
			var l = pubSubListeners[name];
			if(l)
			{
				var len = l.length;
				for(var i=0; i<len; i++)
				{
					responses.push(l[i](name, userData));
				}
			}
			return responses;
		}
	};
})();
